package com.wontube.smr.remote.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.GenreSelectBO;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.TVEpisode;
import com.wontube.smr.client.model.po.TVShow;
import com.wontube.smr.client.model.vo.SeasonEpisodes;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.dao.bo.AnimeSelectDao;
import com.wontube.smr.dao.bo.TVSelectDao;
import com.wontube.smr.web.client.IAnimeWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * 动漫 Handler
 * 
 * @author luobiao@wondershare.cn
 * 
 */
@NoLoginSupport
public final class AnimeWebHandler extends BasicHandler implements IAnimeWebHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listAnimeGenres() {
		return GenreSelectBO.selectRemendedAnimeGenres();
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public TVShowItems selectRelatedAnime(String showId,PagenateInfo pageInfo){
		if(StringUtils.isNullOrEmpty(showId)){
			return null;
		}
		return AnimeSelectDao.selectRelatedAnime(showId, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public TVShowItems listAnimes(QueryItemParams params, PagenateInfo pageInfo) {
		TVShowItems items = AnimeSelectDao.selectAnimes(params, pageInfo);
		//判断是否需要genre
		if(params.isNeedGenres()){
			items.setTvGenres(listAnimeGenres());
		}
		return items;
	}	

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public List<SeasonEpisodes> listTVEpisodeDetail(String showId) {
		if(StringUtils.isNullOrEmpty(showId)){
			return null;
		}
		return TVSelectDao.getAllEpisodes(showId);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 3 * 3600)
	public TVShowItems listAnimeSimulacasts(PagenateInfo pageInfo) {
		return AnimeSelectDao.selectSimulcastAnimes(pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 3 * 3600)
	public TVShowItems listRecentAinmes(PagenateInfo pageInfo) {
		return AnimeSelectDao.selectRecentAnimes(null, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 3 * 3600)
	public TVEpisode getTVEpisode(String id){
		if(StringUtils.isNullOrEmpty(id)){
			return null;
		}
		return TVSelectDao.getTVEpisode(id);
	}

	/*
	 * show -> TVShow
	 * seasons -> List<SeasonEpisodes>
	 * animeGenres -> List<Genre>
     */
	@Override
	@CacheSupport(timeToLiveSeconds = 3 * 3600)
	public Map<String, Object> collectTVDetails(String showId) {
		Map<String, Object> maps = new HashMap<String, Object>();
		if(StringUtils.isNullOrEmpty(showId)){
			return maps;
		}	
				
		//TVShow 
		TVShow show = TVSelectDao.getTVShow(showId);
		if(show == null){
			return maps;
		}
		
		maps.put("show", show);
		
		List<SeasonEpisodes> seasons = TVSelectDao.getAllEpisodes(showId);
		maps.put("seasons", seasons);
		
		maps.put("animeGenres", listAnimeGenres());
		return maps;
	}

	/*
	 * anime -> TVEpisode
	 * seasons -> List<SeasonEpisodes>
	 * animeGenres -> List<Genre>
	 * show -> TVShow
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 3 * 3600)
	public Map<String, Object> collectTVPlayDetail(String epId) {
		Map<String, Object> maps = new HashMap<String, Object>();		
		if(StringUtils.isNullOrEmpty(epId)){
			return maps;
		}	
		
		TVEpisode anime = TVSelectDao.getTVEpisode(epId);
		if(anime == null){
			return maps;
		}	
		maps.put("anime", anime);
		
		List<SeasonEpisodes> seasons = TVSelectDao.getAllEpisodes(anime.getShowId());
		maps.put("seasons", seasons);
		maps.put("animeGenres", listAnimeGenres());
		return maps;
	}
}
